package spring.boot.admin.model;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Data;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.List;

@Data
@TableName("sys_user")
public class SysUser implements UserDetails {

    @TableId(type = IdType.AUTO)
    @JsonSerialize(using = ToStringSerializer.class)
    private Long id;

    private String username;

    private String password;

    private Integer sex;

    private Integer age;

    @TableField("real_name")
    private String realName;

    @TableField("create_time")
    private String createTime;

    private Integer enable;


    /* 以下为非数据库字段 */
    @TableField(exist = false)
    private List<SysMenu> authorities;

    @TableField(exist = false)
    private List<SysRole> roles;


    /* 以下为 security框架的 UserDetails 类默认实现的方法 */
    @Override
    public boolean isEnabled() {
        return enable == 1;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return enable == 1;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }


}
